package com.gljx.web.controller;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gljx.constant.ActivitiKey;
import com.gljx.util.*;
import com.gljx.web.Entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gljx.web.service.CustomLogService;
import com.gljx.web.service.DepartmentService;
import com.gljx.web.service.ExitClazzService;
import com.gljx.web.service.ReturnPremiumService;

@Controller
@RequestMapping("/")
public class ReturnPremiumController {

	//自动注入
	@Autowired
	private ReturnPremiumService returnPremiumService;
	
	//自动注入
	@Autowired
	private DepartmentService departmentService;

	//自动注入
	@Autowired
	private CustomLogService customLogService;
		
	//自动注入
	@Autowired
	private ExitClazzService exitClazzService;
	
	
	
	/**
	 * 查询退款登记
	 * @param request
	 * @return
	 */
	@RequestMapping("/queryReturnPremium/look.action")
	public String queryReturnPremium(HttpServletRequest request,String type){
		PageBean<ReturnPremium> pb = new PageBean<>();
		//获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
		//时间格式化
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		//获取当前时间
		String endTime = sdf.format(new Date());
		//获取前三天
		String beginTime = TimeFormat.getThreeDay();
		try {
			//获取分页
			pb = getPageBean(request,user.getFcompanyid(),beginTime,endTime);
			//获取分页数据
			List<ReturnPremium> returnPremiums = pb.getBeanList();
			for (ReturnPremium returnPremium : returnPremiums) {
				//根据父类id查询子类信息
				List<ReturnPremiumEntry> returnPremiumEntries = returnPremiumService.queryReturnPremiumEntry(returnPremium.getFid(), "");
				//把子类信息放入集合中
				returnPremium.setReturnPremiumEntrys(returnPremiumEntries);
			}
			//合计信息总条数
			String messageAmount="共有"+pb.getTr()+"条信息";
			//把数据传到页面
			request.setAttribute("messageAmount",messageAmount);
			request.setAttribute("beginTime", beginTime);
			request.setAttribute("endTime", endTime);
			request.setAttribute("returnPremiums", returnPremiums);
			request.setAttribute("pb", pb);
			request.setAttribute("flag", type);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "/backHome/financialManagement/returnPremium/returnPremiumList";
	}
	
	/**
	 * 根据条件查询退款登记
	 * @param request
	 * @param studentName 学员姓名
	 * @param beginTime 业务开始时间
	 * @param endTime 业务结束时间
	 * @return
	 */
	@RequestMapping("/queryReturnPremiumByInfo")
	public String queryReturnPremiumByInfo(HttpServletRequest request,String studentName,String beginTime,String endTime){
		PageBean<ReturnPremium> pb = new PageBean<>();
		//获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
		try {
			//去除空格
			studentName = studentName.trim();
			//获取分页
			pb = getPageBean2(request,user.getFcompanyid(),studentName,beginTime,endTime);
			//获取分页数据
			List<ReturnPremium> returnPremiums = pb.getBeanList();
			for (ReturnPremium returnPremium : returnPremiums) {
				//根据父类id查询子类信息
				List<ReturnPremiumEntry> returnPremiumEntries = returnPremiumService.queryReturnPremiumEntry(returnPremium.getFid(),studentName);
				//把子类信息放入集合中
				returnPremium.setReturnPremiumEntrys(returnPremiumEntries);
			}
			//合计信息总条数
			String messageAmount="共有"+pb.getTr()+"条信息";
			//把数据传到页面
			request.setAttribute("messageAmount",messageAmount);
			request.setAttribute("pb", pb);
			request.setAttribute("returnPremiums", returnPremiums);
			request.setAttribute("studentName", studentName);
			request.setAttribute("beginTime", beginTime);
			request.setAttribute("endTime", endTime);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "/backHome/financialManagement/returnPremium/returnPremiumList";
	}
	
	/**
	 * 跳转到添加页面
	 * @param request
	 * @return
	 */
	@RequestMapping("/insertReturnPremium")
	public String insertReturnPremium(HttpServletRequest request,String type){
		//获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
		//获取部门
		Department department = departmentService.queryAdminByFid(user.getFcompanyid());
		//获取最大编码
        /*String fnumber = customLogService.selectFunmber("ct_ds_turnexamcost",user.getFcompanyid());*/
		//获取下一个编码
        String number = GetFnumber.fnumbers("TF");
		//获取当前时间
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		//开始时间
		String time = sdf.format(new Date());
		//获取退学申请表信息
		List<ExitClazz> exitClazzs = exitClazzService.queryDropoutapply(user.getFcompanyid());
		//把数据传到页面
		request.setAttribute("time", time);
		request.setAttribute("exitClazzs", exitClazzs);
		request.setAttribute("type", type);
		request.setAttribute("name", user.getName());
		request.setAttribute("fnumber", number);
		request.setAttribute("department", department);
		return "/backHome/financialManagement/returnPremium/editReturnPremium";
	}
	
	/**
	 * 根据fid查询退款申请
	 * @param id
	 * @return
	 */
	@RequestMapping("/selExitClassByFid")
	@ResponseBody
	public ExitClazz selExitClassByFid(String id){
		//根据fid查询
		ExitClazz exitClazz = exitClazzService.queryDropoutapplyByFid(id);
		return exitClazz;
	}
	
	/**
	 * 根据名称查询退款申请
	 * @param request
	 * @param name
	 * @return
	 */
	@RequestMapping("/selExitClassByName")
	@ResponseBody
	public List<ExitClazz> selExitClassByName(HttpServletRequest request,String name){
		//获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
		List<ExitClazz> exitClazzs = new ArrayList<>();
		//查询数据
		if(name == null || "".equals(name)){
			exitClazzs = exitClazzService.queryDropoutapply(user.getFcompanyid());
		}else{
			exitClazzs = exitClazzService.queryDropoutapplyByName(user.getFcompanyid(), name);
		}
		return exitClazzs;
	}
	/**
	 * 添加退款登记
	 * @param request
	 * @param returnPremium
	 * @param fid 用于判断是否是添加和修改
	 * @return
	 */
	@RequestMapping("/editReturnPremium")
	public String editReturnPremium(HttpServletRequest request,ReturnPremium returnPremium,String fid){
		//获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
		//控制单元id
		returnPremium.setFcontrolunitid(user.getFcompanyid());
		//公司id
		returnPremium.setCfficompanyid(user.getFcompanyid());
		//获取当前时间
		String time = TimeFormat.timeFormat(new Date());
		//操作人id
		returnPremium.setFhandlerid(user.getFid());
		/**获取子类信息集合**/
		//序列
		String[] fseq = request.getParameterValues("fseq");
		//学员编码
		String[] cfstudentno = request.getParameterValues("cfstudentno");
		//学员id
		String[] cfstudentid = request.getParameterValues("cfstudentid");
		//学员姓名
		String[] studentName = request.getParameterValues("studentName");
		//身份证地址
		String[] cfidcardaddress = request.getParameterValues("cfidcardaddress");
		//申请准驾车型代号
		String[] cfsqzjcxdh = request.getParameterValues("cfsqzjcxdh");
		//应收合计
		String[] cfreceivable = request.getParameterValues("cfreceivable");
		//实收合计
		String[] cfreceived = request.getParameterValues("cfreceived");
		//优惠合计
		String[] cfprivilege = request.getParameterValues("cfprivilege");
		//欠费合计
		String[] cfarrearage = request.getParameterValues("cfarrearage");
		//支出合计
		String[] cfexpenditure = request.getParameterValues("cfexpenditure");
		//可退合计
		String[] cfythj = request.getParameterValues("cfythj");
		//现金退费
		String[] cfcash = request.getParameterValues("cfcash");
		//刷卡退费
		String[] cfcard = request.getParameterValues("cfcard");
		//银行转账
		String[] cfyhzz = request.getParameterValues("cfyhzz");
		//退费总额
		String[] cfamount = request.getParameterValues("cfamount");
		//摘要
		String[] cfdesc = request.getParameterValues("cfdesc");
		//添加或修改
		returnPremiumService.editReturnPremium(returnPremium,fseq,cfstudentno,cfstudentid,cfidcardaddress,cfsqzjcxdh,cfreceivable,
				cfreceived,cfprivilege,cfarrearage,cfexpenditure,cfythj,cfcash,cfcard,cfyhzz,cfamount,cfdesc,user,time);
		if(fid == null || "".equals(fid)){
			List<CustomLog> customLogs = new ArrayList<>();
			for (int i = 0; i < cfstudentid.length; i++) {
				CustomLog customLog = new CustomLog();
				customLog.setFid(GetFid.UUIDIntoFid());
				customLog.setFcreatetime(time);
				customLog.setFcreatorid(user.getFid());
				customLog.setCfstudentid(cfstudentid[i]);
                customLog.setFnameL2("退款登记");
                customLog.setFdescriptionL2("新增学员:" + cfstudentno[i] + studentName[i] + "退费金额：" + cfamount[i] + "单据编号:" + returnPremium.getFnumber());
                //控制单元
				customLog.setFcontrolunitid(user.getFcompanyid());
				customLogs.add(customLog);
			}
			//添加日志
			customLogService.insertCustomLog(customLogs);
			//customLogService.addLog("财务管理", time+"对退款登记进行添加操作", null, user.getFid());
			//重定向到添加页面
			return "redirect:/DrivingSys/insertReturnPremium?type=3";
		}else{
			List<CustomLog> customLogs = new ArrayList<>();
			for (int i = 0; i < cfstudentid.length; i++) {
				CustomLog customLog = new CustomLog();
				customLog.setFid(GetFid.UUIDIntoFid());
				customLog.setFcreatetime(time);
				customLog.setFcreatorid(user.getFid());
				customLog.setCfstudentid(cfstudentid[i]);
                customLog.setFnameL2("退款登记");
                customLog.setFdescriptionL2("修改学员:" + cfstudentno[i] + studentName[i] + "退费金额：" + cfamount[i] + "单据编号:" + returnPremium.getFnumber());
                //控制单元
				customLog.setFcontrolunitid(user.getFcompanyid());
				customLogs.add(customLog);
			}
			//添加日志
			customLogService.insertCustomLog(customLogs);
			//customLogService.addLog("财务管理", time+"对退款登记进行修改操作", null, user.getFid());
			//重定向到查询页面
			return "redirect:/DrivingSys/queryReturnPremium/look.action?type=4";
		}
		
	}
	
	/**
	 * 根据fid查询
	 * @param request
	 * @param fid
	 * @return
	 */
	@RequestMapping("/queryReturnPremiumByFid")
	public String queryReturnPremiumByFid(HttpServletRequest request,String fid){
		//获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
		//查询父类信息
		ReturnPremium returnPremium = returnPremiumService.queryReturnPremiumByFid(fid);
		//查询子类信息
		List<ReturnPremiumEntry> returnPremiumEntrys = returnPremiumService.queryReturnPremiumEntry(fid, "");
		List<ExitClazz> exitClazzs = exitClazzService.queryDropoutapply(user.getFcompanyid());
        //查询是否是经办人
        Axamine axamine = new Axamine();
        boolean state = axamine.selectAuditPerson(returnPremium.getFnumber());
        //把数据传到页面
        request.setAttribute("AuditState", state);
        request.setAttribute("exitClazzs", exitClazzs);
		request.setAttribute("returnPremium", returnPremium);
		request.setAttribute("returnPremiumEntrys", returnPremiumEntrys);
        request.setAttribute("key", ActivitiKey.PUBLIC_KEY);
        return "/backHome/financialManagement/returnPremium/editReturnPremium";
    }
	
	/**
	 * 根据fid删除
	 * @param request
	 * @param fid 单个
	 * @param deleteId 多个
	 * @return
	 */
	@RequestMapping("/deleteReturnPremium")
    public String deleteReturnPremium(HttpServletRequest request, String fid, String[] deleteId, String[] fnumber, String fnumber2) {
        //获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
		String time = TimeFormat.timeFormat(new Date());
        ActitiviUtil actitiviUtil = new ActitiviUtil();
        List<CustomLog> customLogs = new ArrayList<>();
		List<ReturnPremiumEntry> returnPremiumEntrys = new ArrayList<>();
		if (fid == null) {
			for (int i = 0; i < deleteId.length; i++) {
                //删除流程
                actitiviUtil.deleteTask(ActivitiKey.PUBLIC_KEY, fnumber[i]);
                returnPremiumEntrys = returnPremiumService.queryReturnPremiumEntry(deleteId[i], "");
				for (ReturnPremiumEntry returnPremiumEntry : returnPremiumEntrys) {
					CustomLog customLog2 = new CustomLog();
					customLog2.setFid(GetFid.UUIDIntoFid());
					customLog2.setFcreatetime(time);
					customLog2.setFcreatorid(user.getFid());
					customLog2.setCfstudentid(returnPremiumEntry.getCfstudentid());
                    customLog2.setFnameL2("退款登记");
                    customLog2.setFdescriptionL2("删除学员:" + returnPremiumEntry.getCfstudentno() + returnPremiumEntry.getStudentName());
                    //控制单元
					customLog2.setFcontrolunitid(user.getFcompanyid());
					customLogs.add(customLog2);
				}
			}
		} else {
			returnPremiumEntrys = returnPremiumService.queryReturnPremiumEntry(fid, "");
            //删除流程
            actitiviUtil.deleteTask(ActivitiKey.PUBLIC_KEY, fnumber2);
            for (ReturnPremiumEntry returnPremiumEntry : returnPremiumEntrys) {
				CustomLog customLog = new CustomLog();
				customLog.setFid(GetFid.UUIDIntoFid());
				customLog.setFcreatetime(time);
				customLog.setFcreatorid(user.getFid());
				customLog.setCfstudentid(returnPremiumEntry.getCfstudentid());
                customLog.setFnameL2("退款登记".trim());
                customLog.setFdescriptionL2("删除学员:" + returnPremiumEntry.getCfstudentno() + returnPremiumEntry.getStudentName());
                //控制单元
				customLog.setFcontrolunitid(user.getFcompanyid());
				customLogs.add(customLog);
			}
		}
		//customLogService.addLog("财务管理", TimeFormat.timeFormat(new Date())+"对退款登记进行修改操作", null, user.getFid());
		//删除
		returnPremiumService.deleteReturnPremium(fid,deleteId);
		//添加日志
		customLogService.insertCustomLog(customLogs);
		//重定向到查询页面
		return "redirect:/DrivingSys/queryReturnPremium/look.action";
	}
	
	/**
	 * 审核，反审核
	 * @param request
	 * @param fid
	 * @param type 1：审核，2：反审核
	 * @return
	 */
	@RequestMapping("/auditReturnPremium")
	@ResponseBody
    public int auditReturnPremium(HttpServletRequest request, String fid, String type, String message, String state, String fnumber,
                                  @RequestParam(value = "studentName[]", defaultValue = "") String[] studentName,
                                  @RequestParam(value = "studentId[]", defaultValue = "") String[] studentId,
                                  @RequestParam(value = "cfstudentno[]", defaultValue = "") String[] cfstudentno) {
        //获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
        int i = returnPremiumService.auditReturnPremium(fid, type, user.getFid(), message, state, fnumber);
        String ms;
        if ("1".equals(type)) {
            ms = "审核";
        } else {
            ms = "反审核";
        }
        //添加日志
        customLogService.batchInsertState(studentName, studentId, "退款登记", cfstudentno, "单据编号:" + fnumber, ms);
        return i;
	}

    /**
     * 提交
     * @param fid
     * @param
     * @return
     */
    @RequestMapping("/submitReturnPremium")
    @ResponseBody
    public int submitReturnPremium(String fid, HttpServletRequest request, String fnumber, String auditState,
                                   @RequestParam(value = "studentName[]", defaultValue = "") String[] studentName,
                                   @RequestParam(value = "studentId[]", defaultValue = "") String[] studentId,
                                   @RequestParam(value = "cfstudentno[]", defaultValue = "") String[] cfstudentno) {
        //获取操作人信息
        ReturnPremium returnPremium = new ReturnPremium();
        //获取操作人信息
        User user = (User) request.getSession().getAttribute("user");
        //fid
        returnPremium.setFid(fid);
        //修改人
        returnPremium.setFlastupdateuserid(user.getFid());
        returnPremium.setCfbillstate(4L);
        //创建流程
        Axamine axamine = new Axamine();
        axamine.submissionProcess(fnumber);
		//添加日志
        customLogService.batchInsertState(studentName, studentId, "退款登记", cfstudentno, "单据编号:" + fnumber, "提交");
        return returnPremiumService.modifyReturnPremium(returnPremium);
    }

    @SuppressWarnings("unchecked")
	/**
	 * 返回PageBean实体类  包括记录总数，页码，每页记录集等  dmz 20160921
	 * @return 返回PageBean实体类 
	 * @throws Exception
	 */
	public PageBean<ReturnPremium> getPageBean2(HttpServletRequest request,String companyId,String studentName,String beginTime,String endTime) throws Exception
	{

		int offset;//从第几条数据开始加载
		if(request.getParameter("pager.offset")==null){  
			offset=0;  
		}else{  
			offset=Integer.parseInt(request.getParameter("pager.offset"));  
		}	
		//拼接SQL语句
		StringBuffer psql = new  StringBuffer();
		psql.append("select distinct t.fid,t.fnumber,t.fdescription,to_char(t.fbizdate,'yyyy-MM-dd')fbizdate,u.name createName,us.name auditName,t.cfbillstate ");
		psql.append("from CT_DS_RETURNPREMIUM t left join t_e_user u  on t.fcreatorid=u.fid left join t_e_user us on t.fauditorid = us.fid ");
		psql.append("left join ct_ds_returnpremiumentry r on t.fid = r.fparentid left join ct_ds_student s on r.cfstudentid = s.fid");
		psql.append(" where t.fcontrolunitid = '"+companyId+"'");
		if("".equals(studentName)){
			
		}else{
			psql.append(" and s.fname_l2 like '%"+studentName+"%'");
		}
		if("".equals(beginTime) || "".equals(endTime)){

		}else{
			psql.append("  and t.fbizdate between to_date('"+beginTime+"','YYYY/MM/DD') and to_date('"+endTime+"','YYYY/MM/DD')");
		}
        psql.append(" order by to_char(t.fbizdate,'yyyy-MM-dd') desc");
        int psize = 100; //每页显示数
        int pindex=offset/psize;//+1  页码从0开始
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap= returnPremiumService.selectPage(pindex, psql.toString(), psize);
		PageBean<ReturnPremium> pb=new PageBean<>();
		pb.setPc(pindex); //页码
		Map<String, Object> paramMapCount=getPageBeanCount(psql.toString()); //取记录总数
		pb.setTr((int)paramMapCount.get("Pcount")); //记录总数
		pb.setPs(psize);
		pb.setBeanList((List<ReturnPremium>)paramMap.get("v_cur")); //每页数据集
		return pb;
	}
	
	
	@SuppressWarnings("unchecked")
	/**
	 * 返回PageBean实体类  包括记录总数，页码，每页记录集等  dmz 20160921
	 * @return 返回PageBean实体类 
	 * @throws Exception
	 */
	public PageBean<ReturnPremium> getPageBean(HttpServletRequest request,String companyId,String beginTime,String endTime) throws Exception
	{

		int offset;//从第几条数据开始加载
		if(request.getParameter("pager.offset")==null){  
			offset=0;  
		}else{  
			offset=Integer.parseInt(request.getParameter("pager.offset"));  
		}	
		//拼接SQL语句
		StringBuffer psql = new  StringBuffer();
		psql.append("select t.fid,t.fnumber,t.fdescription,to_char(t.fbizdate,'yyyy-MM-dd')fbizdate,u.name createName,us.name auditName,t.cfbillstate from CT_DS_RETURNPREMIUM t ");
		psql.append("left join t_e_user u  on t.fcreatorid=u.fid left join t_e_user us on t.fauditorid = us.fid where t.fcontrolunitid = '"+companyId+"'");
		psql.append(" and t.fbizdate between to_date('"+beginTime+"','YYYY/MM/DD') and to_date('"+endTime+"','YYYY/MM/DD')");
        psql.append(" order by to_char(t.fbizdate,'yyyy-MM-dd') desc");
        int psize = 10; //每页显示数
        int pindex=offset/psize;//+1  页码从0开始
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap= returnPremiumService.selectPage(pindex, psql.toString(), psize);
		PageBean<ReturnPremium> pb=new PageBean<>();
		pb.setPc(pindex); //页码
		Map<String, Object> paramMapCount=getPageBeanCount(psql.toString()); //取记录总数
		pb.setTr((int)paramMapCount.get("Pcount")); //记录总数
		pb.setPs(psize);
		pb.setBeanList((List<ReturnPremium>)paramMap.get("v_cur")); //每页数据集
		return pb;
	}

	/**
	 * 取记录总数 dmz 20160921
	 * @param psql  SQL语句
	 * @return Map
	 * @throws Exception
	 */
	public Map<String, Object> getPageBeanCount(String psql) throws Exception
	{

		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap=returnPremiumService.selectPageCount(psql);
		return paramMap;		
	}

    /**
     * 导出Excel
     *
     * @param response
     * @param request
     * @param studentName
     * @param beginTime
     * @param endTime
     */
    @RequestMapping("/importReturnPremium")
    public void importReturnPremium(HttpServletResponse response, HttpServletRequest request, String studentName, String beginTime, String endTime) {
        //获取操作人信息
        User user = (User) request.getSession().getAttribute("user");
        //定义接收类型
        List<Object> data = new ArrayList<>();
        //查询数据
        List<ReturnPremiumEntry> returnPremiumEntrys = returnPremiumService.selectReturnPremiumByInfo(user.getFcompanyid(), studentName.trim(), beginTime, endTime);
        for (ReturnPremiumEntry returnPremiumEntry : returnPremiumEntrys) {
            data.add(returnPremiumEntry.getFnumber());
            data.add(returnPremiumEntry.getFbizdate());
            data.add(returnPremiumEntry.getFdescription());
            data.add(returnPremiumEntry.getShName());
            data.add(returnPremiumEntry.getZdrName());
            data.add(stateToString.billsState(returnPremiumEntry.getCfbillstate()));
            data.add(returnPremiumEntry.getStudentName());
            data.add(returnPremiumEntry.getCfstudentno());
            data.add(returnPremiumEntry.getCfsqzjcxdh());
            data.add(returnPremiumEntry.getCfexpenditure());
            data.add(returnPremiumEntry.getCfamount());
            data.add(returnPremiumEntry.getCfdesc());
            data.add(returnPremiumEntry.getCfreceivable());
            data.add(returnPremiumEntry.getCfcash());
            data.add(returnPremiumEntry.getCfcard());
            data.add(returnPremiumEntry.getCfreceived());
            data.add(returnPremiumEntry.getCfprivilege());
            data.add(returnPremiumEntry.getCfarrearage());
            data.add(returnPremiumEntry.getCfidcardaddress());
            data.add(returnPremiumEntry.getCfythj());
            data.add(returnPremiumEntry.getCfyhzz());
        }
        //头部数据
        String head[] = {"单据编码", "业务日期", "摘要", "审核人名称", "制单人名称", "单据状态", "学员", "学员编码", "申请准驾车型代号", "支出合计", "退费总额", "摘要", "应收合计",
                "现金退费", "刷卡退费", "实收合计", "优惠合计", "欠费合计", "身份证地址", "可退合计", "银行转账"};
        //长度
        Integer length[] = {6500, 3000, 4000, 3000, 3000, 3000, 2600, 6200, 2600, 2600, 2600, 4500, 2600, 2600, 2600, 2600, 2600, 2600, 6000, 2600, 2600};


        List<Object> data2= listForSum(data,head,9,2,1,6);
        try {
            //导出Excel
            CommonExport.importExcelSum(response, "退款登记" + TimeFormat.getNowTime2() + ".xls", "退款登记", data, head, length,data2);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
	/**
	 *统计方法
	 * @param list1 完成遍历的集合
	 * @param head  表头
	 * @param num   求和的第一个字段的下标
	 * @param sum  需要增加多少个字段
	 * @return
	 */
	public List<Object> listForSum(List<Object> list1,String[] head,int num,int sum, int nextCount,int nextSum){

		Double money=0.0;

		List<Object>list=new ArrayList<>();
		DecimalFormat df = new DecimalFormat("#.00");
		list.add("合计：");

		for (int i = 0; i < num - 1; i++) {
			list.add(" ");
		}

		for(int i=0;i<sum;i++){

			for (int j = 0; j < list1.size() / head.length; j++) {
				if (list1.get(num + head.length*j)==null || "".equals(list1.get(num + head.length * j))) {
					money += 0.00;
				} else {
					money += Double.parseDouble(list1.get(num + head.length * j) + "");
				}
			}
			if(money==0.00||money==null){
				list.add("0.00");
			}else{
				if(".00".equals(df.format(money))){
					list.add("");
				}else{
					list.add(df.format(money));
				}
			}
			num++;
			money = 0.00;
		}
			list.add("");
		   num+=nextCount;
		for(int i=0;i<nextSum;i++){

			for (int j = 0; j < list1.size() / head.length; j++) {
				if (list1.get(num + head.length*j)==null || "".equals(list1.get(num + head.length * j))) {
					money += 0.00;
				} else {
					money += Double.parseDouble(list1.get(num + head.length * j) + "");
				}
			}
			if(money==0.00||money==null){
				list.add("0.00");
			}else{
				if(".00".equals(df.format(money))){
					list.add("");
				}else{
					list.add(df.format(money));
				}

			}
				 num++;
				 money = 0.00;
		}
		list.add("");
		num+=nextCount;
		for(int i=0;i<2;i++){

			for (int j = 0; j < list1.size() / head.length; j++) {
				if (list1.get(num + head.length*j)==null || "".equals(list1.get(num + head.length * j))) {
					money += 0.00;
				} else {
					money += Double.parseDouble(list1.get(num + head.length * j) + "");
				}
			}
			if(money==0.00||money==null){
				list.add("0.00");
			}else{
				if(".00".equals(df.format(money))){
					list.add("");
				}else{
					list.add(df.format(money));
				}

			}
			num++;
			money = 0.00;
		}


		return list;
	}

}
